<?php

class SearchBehavior extends CActiveRecordBehavior{

	/**
	 * 分页数
	 * Enter description here ...
	 * @var int
	 */
	public $pageSize = 10;
	/**
	 * criteria条件
	 * Enter description here ...
	 * @var array
	 */
	public $condition = array();
	/**
	 * 定义一个搜索类，需要基础SearchBaseModel
	 * Enter description here ...
	 * @var string
	 */
	public $searchModel = '';
	
	public $timeSearch;
	public $priceSearch;
	
	public $selectTime;
	
	public $begin_time;
	public $end_time;
	public $price;
	public $noSearchAttributes = array();
	
	public $filterAttributes = array('name','desc','title','sn');
	public function search() {
		$criteria = $this->owner->getDbCriteria();
		$criteria->mergeWith(new CDbCriteria($this->condition));
		$model = $this->owner;
		foreach ($model->attributeNames() as $one) {
			$like = '';
			foreach ($this->filterAttributes as $val) {
				$preg = "/{$val}$/i";
				$like = preg_match($preg, $one, $like);
				if($like)
					continue;
			}
			if (in_array($one, $this->noSearchAttributes)) continue;
			$criteria->compare($model->tableAlias.'.'.$one, $model->$one, !is_numeric($model->$one)||$like);
		}
		if (!empty($this->searchModel)) {
			$searchModel = new $this->searchModel($model, $criteria);
			$criteria = $searchModel->getCriteria();
		}
		
		if(!empty($this->timeSearch))
			$criteria = $this->searchTime($criteria);
		if(!empty($this->owner->selectTime))
			$criteria = $this->searchSeleteTime($criteria);
		if(!empty($this->price))
			$criteria = $this->searchPrice($criteria);
		return new CActiveDataProvider($model, array(
			'criteria'=>$criteria,
			'pagination' => array(
				'pageSize' => empty(Yii::app()->controller->action->pageSize) ? $this->pageSize : Yii::app()->controller->action->pageSize,
			)
		));
	}
	
	public function searchSeleteTime($criteria) {
		$beginTime = '';
		switch ($this->owner->selectTime) {
			case 'lastDay':
				$beginTime = date('Y-m-d',strtotime('-1 day'));
				break;
			case 'lastWeek':
				$beginTime = date('Y-m-d',strtotime('-1 week'));
				break;
			case 'lastMonth':
				$beginTime = date('Y-m-d',strtotime('-1 month'));
				break;
			case 'lastThreeMonth':
				$beginTime = date('Y-m-d',strtotime('-3 month'));
				break;
			default:
				break;
			
		}
		$endTime = date('Y-m-d H:i');
		$criteria->addBetweenCondition($this->timeSearch,$beginTime, $endTime);
		return $criteria;
	}
	
	public function searchTime($criteria) {
		$beginTime = $this->owner->begin_time;
		$endTime = $this->owner->end_time;
		$attribute = $this->timeSearch;
		
		if (!$this->owner->hasAttribute($attribute)) return $criteria;
		
		if (!empty($beginTime) && !empty($endTime))
			$criteria->addBetweenCondition($attribute,$beginTime, $endTime);
		else if (!empty($beginTime))
			$criteria->addCondition("{$attribute} > '{$beginTime}'");
		else if (!empty($endTime))
			$criteria->addCondition("{$attribute} < '{$endTime}'");
		return $criteria;
	}
	
	public function searchPrice($criteria) {
		$alias = 't';
		if (is_array($this->priceSearch)) {
			$attribute = $this->priceSearch['attribute'];
			if (!empty($this->priceSearch['criteria'])) {
				$criteria->mergeWith(new CDbCriteria($this->priceSearch['criteria']));
			}
			$alias = !empty($this->priceSearch['alias']) ? $this->priceSearch['alias'] : $alias;
		} else 
			$attribute = $this->priceSearch;
		
		if (empty($attribute)) return $criteria;
		
		$price = explode('-', $this->price);
		if (count($price) == 1)
			$criteria->addCondition("{$alias}.{$attribute} {$price[0]}");
		else 
			$criteria->addBetweenCondition($alias.'.'.$attribute, $price[0], $price[1]);
		
		return $criteria;
	}
}

?>